import axios from 'axios';
import { message } from 'ant-design-vue';
const [messageApi, contextHolder] = message.useMessage();

console.log(import.meta.env);
// 实例化 axios
const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
});

// request 拦截器
service.interceptors.request.use(
  config => {
    // if (localStorage.getItem('token')) {
    //   // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
    //   config.headers['X-Token'] = localStorage.getItem('token');
    // }
    // 表单提交处理
    if (config.method === 'post' && config.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
      config.data = qs.stringify(config.data);
    }
    return config;
  },
  error => {
    // Do something with request error
    console.log(error); // for debug
    Promise.reject(error);
  }
);

// 响应器
service.interceptors.response.use(response => {
  const res = response.data;
  console.log({ response, res });
  return res;
});

export default service;
